<template>
	<view>
		<!-- 分配客户经理 -->
		<u-picker
			title="客户经理"
			:show="fpkhShow2"
			:columns="[accountManagerList]"
			keyName="name"
			@close="chooseKhjlFunc2"
			@cancel="chooseKhjlFunc2"
			@confirm="fpkhConfirm"
			closeOnClickOverlay
		></u-picker>
		<u-popup :show="fpkhShow" @close="close('projects_assignAccountManager')" mode="bottom" closeable round="10">
			<view class="fp-box-title">分配客户经理</view>
			<view class="fp-box">
				<u--form labelPosition="top" :model="pfkhjlForm" labelWidth="auto">
					<u-form-item required label="客户经理" borderBottom @click="chooseKhjlFunc">
						<u--input v-model="pfkhjlForm.account_manager_name" readonly border="none" placeholder="选择客户经理" suffixIcon="arrow-right"></u--input>
					</u-form-item>
					<u-form-item label="备注">
						<u--textarea v-model="pfkhjlForm.remark"></u--textarea>
					</u-form-item>
				</u--form>
				<u-button type="primary" text="提交" color="#3B75FF" :customStyle="{ marginTop: '30rpx' }" @click="assignAccountManager"></u-button>
			</view>
		</u-popup>
		<!-- 更换客户经理 -->
		<changeManCom @closeFunc="changCloseFunc" :show="changShow" :type="1" :projectId="oper_item.id" @changeManSuccessFunc="changeManSuccessFunc"></changeManCom>
		<!-- 项目回退 -->
		<u-picker
			title="项目回退"
			:show="xmhtShow"
			:columns="[backOptions]"
			keyName="label"
			@close="xmhtClose"
			@cancel="xmhtClose"
			@confirm="xmhtConfirm"
			closeOnClickOverlay
		></u-picker>
		<!-- 发起结算单 -->
		<u-popup :show="fqjsShow" @close="close('projects_confirmIsChangeItem')" mode="bottom" closeable round="10">
			<view class="fp-box">
				<view class="fp-box-title">发起结算单</view>
				<u--form labelPosition="top" :model="fqjsForm" labelWidth="auto">
					<u-form-item label="备注" required>
						<u--textarea v-model="fqjsForm.remark"></u--textarea>
					</u-form-item>
				</u--form>
				<u-button type="primary" text="提交" color="#3B75FF" :customStyle="{ marginTop: '30rpx' }" @click="confirmIsChangeItem"></u-button>
			</view>
		</u-popup>
		<!-- 项目启动 -->
		<u-popup :show="xmqdShow" @close="close('projects_chooseSurveySite')" mode="bottom" closeable round="10">
			<view class="fp-box">
				<view class="fp-box-title">项目启动</view>
				<u--form labelPosition="top" :model="xmqdForm" labelWidth="auto">
					<u-form-item label="是否勘场" required>
						<u-radio-group v-model="xmqdForm.survey_site" @change="surveySiteFunc">
							<u-radio :customStyle="{ marginRight: '16px' }" label="无需勘场" :name="1"></u-radio>
							<u-radio :customStyle="{ marginRight: '16px' }" label="实地勘场" :name="2"></u-radio>
							<u-radio :customStyle="{ marginRight: '16px' }" label="勘场出图" :name="3"></u-radio>
						</u-radio-group>
					</u-form-item>
					<u-form-item label="约定勘场日期" required>
						<u--input
							v-if="xmqdForm.survey_site == 1"
							disabled
							customStyle="padding:10rpx;"
							v-model="xmqdForm.survey_date"
							readonly
							border="none"
							placeholder="选择约定勘场日期"
							suffixIcon="arrow-right"
						></u--input>
						<picker mode="date" :value="xmqdForm.survey_date" @change="dateConfirmFunc" v-else>
							<u--input
								customStyle="padding:10rpx;"
								v-model="xmqdForm.survey_date"
								readonly
								border="none"
								placeholder="选择约定勘场日期"
								suffixIcon="arrow-right"
							></u--input>
						</picker>
					</u-form-item>
					<u-form-item label="设计师" required @click="designerFunc">
						<u--input
							:disabled="xmqdForm.survey_site == 1"
							customStyle="padding:10rpx;"
							v-model="xmqdForm.designer_name"
							readonly
							border="none"
							placeholder="选择设计师"
							suffixIcon="arrow-right"
						></u--input>
					</u-form-item>
				</u--form>
				<u-button type="primary" text="提交" color="#3B75FF" :customStyle="{ marginTop: '30rpx' }" @click="confirmStartFunc"></u-button>
			</view>
		</u-popup>
		<!-- 设计师 -->
		<u-picker
			ref="designer"
			closeOnClickOverlay
			:show="designerShow"
			:columns="[designerList]"
			@confirm="designerConfirmFunc"
			@cancel="designerCloseFunc"
			@close="designerCloseFunc"
			keyName="name"
			title="设计师列表"
		></u-picker>
	</view>
</template>

<script>
import { backdata1, backdata2, backdata3 } from '@/utils/backData';
export default {
	name: 'projectOperate',
	props: {
		value: {
			type: String,
			default: ''
		},
		oper_item: {
			type: Object,
			default: {}
		}
	},
	data() {
		return {
			fpkhShow: false, //分配客户经理弹窗
			fpkhShow2: false,
			accountManagerList: [], //客户经理列表
			pfkhjlForm: {
				account_manager_id: '',
				account_manager_name: '',
				remark: ''
			},
			changShow: false, //更换客户经理
			backOptions: [], //可回退列表
			xmhtShow: false, //项目回退弹窗
			fqjsShow: false, //发起结算弹窗
			fqjsForm: {
				remark: ''
			}, //发起结算数据
			// 项目启动
			xmqdShow: false,
			xmqdForm: {
				survey_site: 1,
				designer_id: '',
				survey_date: '',
				designer_name: ''
			},
			designerList: [], //设计师列表
			designerShow: false //设计师列表弹窗
		};
	},
	watch: {
		value(val) {
			let that = this;
			switch (val) {
				// 项目启动
				case 'projects_chooseSurveySite':
					that.xmqdShow = true;
					that.getAdminList();
					break;
				// 项目完善
				case 'projects_updProject':
					uni.navigateTo({
						url: '/pagesC/projectManagement/addProject/addProject?id=' + that.oper_item.id
					});
					that.close('projects_updProject');
					break;
				// 项目取消
				case 'projects_projectCancellation':
					uni.navigateTo({
						url: '/pagesC/projectManagement/qxProject/qxProject?id=' + that.oper_item.id
					});
					that.close('projects_projectCancellation');
					break;
				// 分配客户经理
				case 'projects_assignAccountManager':
					that.get_account_manager();
					break;
				// 申请报价
				case 'project_sqbj':
					uni.navigateTo({
						url: '/pagesC/projectManagement/sqbjCom/sqbjCom?id=' + that.oper_item.id
					});
					that.close('project_sqbj');
					break;
				// 对客报价
				case 'project_dkbj':
					uni.showModal({
						title: '提示',
						content: '是否将报价单推送给客户?',
						showCancel: true,
						success(res) {
							if (res.confirm) {
								uni.showLoading({
									mask: true
								});
								that.$Apipc
									.toCustomerQuotation({
										project_id: that.oper_item.id,
										remark: '',
										old_amount: '',
										new_amount: '',
										discount_amount: ''
									})
									.then((res) => {
										console.log(res, '对客报价');
										uni.hideLoading();
										if (res.code == 200) {
											that.$emit('submit', that.value);
										}
										uni.showToast({
											title: res.message,
											icon: res.code == 200 ? 'success' : 'error'
										});
									});
							}
						}
					});
					that.close('project_dkbj');
					break;
				// 重新报价
				case 'projects_reQuote':
					uni.showModal({
						title: '提示',
						content: '确认回退至造价员报价?',
						showCancel: true,
						success(res) {
							if (res.confirm) {
								uni.showLoading({
									mask: true
								});
								that.$Apipc
									.reQuote({
										project_id: that.oper_item.id
									})
									.then((res) => {
										console.log(res, '重新报价');
										uni.hideLoading();
										if (res.code == 200) {
											that.$emit('submit', that.value);
										}
										uni.showToast({
											title: res.message,
											icon: res.code == 200 ? 'success' : 'error'
										});
									});
							}
						}
					});
					that.close('projects_reQuote');
					break;
				// 重新结算
				case 'projects_reSettlement':
					uni.showModal({
						title: '提示',
						content: '确认回退至出具结算单?',
						showCancel: true,
						success(res) {
							if (res.confirm) {
								uni.showLoading({
									mask: true
								});

								that.$Apipc
									.projectRollback({
										status: 1130,
										project_id: that.oper_item.id,
										remark: ''
									})
									.then((res) => {
										console.log(res, '回退重新结算');
										uni.hideLoading();
										if (res.code == 200) {
											that.$emit('submit', that.value);
										}
										uni.showToast({
											icon: res.code == 200 ? 'success' : 'error',
											title: res.code == 200 ? '处理成功' : res.message
										});
									})
									.catch((err) => {
										uni.hideLoading();
										uni.showToast({
											icon: 'error',
											title: err.message
										});
									});
							}
						}
					});
					that.close('projects_reSettlement');
					break;
				// 项目回退
				case 'projects_projectRollback':
					let backData = '';
					switch (Number(that.oper_item.survey_site)) {
						case 1:
							backData = backdata1;
							break;
						case 2:
							backData = backdata2;
							break;
						case 3:
							backData = backdata3;
							break;
						default:
					}
					let backOptions = [];
					backData.forEach((item) => {
						if (Number(item.value) < Number(that.oper_item.status)) {
							backOptions.push(item);
						}
					});
					if (!backOptions.length) {
						uni.showToast({
							title: '当前项目无可回退项',
							icon: 'none'
						});
						this.xmhtClose();
						return;
					}
					console.log(backOptions);
					this.backOptions = backOptions;
					this.xmhtShow = true;
					break;
				// 更换客户经理
				case 'common_updProjectLeader':
					that.changShow = true;
					break;
				// 发起结算单
				case 'projects_confirmIsChangeItem':
					this.fqjsShow = true;
					break;
				// 删除项目
				case 'projects_delProject':
					uni.showModal({
						title: '提示',
						content: `确认删除“${that.oper_item.project_name}”吗？`,
						showCancel: true,
						success(res) {
							if (res.confirm) {
								that.delProject([that.oper_item.id]);
							} else {
								that.close(val);
							}
						}
					});
					break;
				default:
			}
		}
	},
	methods: {
		// 是否勘场选择
		surveySiteFunc(e) {
			this.xmqdForm = {
				survey_site: e,
				designer_id: '',
				survey_date: '',
				designer_name: ''
			};
		},
		// 设计师弹窗关闭
		designerCloseFunc() {
			this.designerShow = false;
			this.xmqdShow = true;
		},
		// 设计师弹窗选择确认
		designerConfirmFunc(e) {
			this.xmqdForm.designer_name = e.value[0].name;
			this.xmqdForm.designer_id = e.value[0].id;
			this.designerShow = false;
			this.xmqdShow = true;
		},
		// 设计师列表
		getAdminList() {
			var obj = {
				page: 1,
				num: 9999,
				keyword: '',
				province_id: '',
				city_id: '',
				county_id: '',
				identity: 1,
				position_id_arr: [3]
			};
			this.$Apipc
				.getAdminList(obj)
				.then((res) => {
					console.log(res, '设计师列表');
					if (res.code == 200) {
						this.designerList = res.data.list;
					}
				})
				.catch((err) => {});
		},
		// 选择设计师
		designerFunc() {
			if (this.xmqdForm.survey_site == 1) return;
			this.designerShow = true;
			this.xmqdShow = false;
		},
		// 约定勘场日期选择
		dateConfirmFunc(e) {
			this.xmqdForm.survey_date = e.detail.value;
		},
		// 项目启动
		confirmStartFunc() {
			if (this.xmqdForm.survey_site != 1 && !this.xmqdForm.survey_date) {
				uni.showToast({
					title: '请选择约定勘场日期',
					icon: 'error'
				});
				return;
			}
			if (this.xmqdForm.survey_site != 1 && !this.xmqdForm.designer_id) {
				uni.showToast({
					title: '请选择设计师',
					icon: 'error'
				});
				return;
			}
			uni.showLoading({
				mask: true
			});
			let designerData = null;
			if (this.xmqdForm.designer_id) {
				designerData = this.designerList.find((item) => {
					return item.id == this.xmqdForm.designer_id;
				});
			}
			this.$Api
				.chooseSurveySite({
					project_id: this.oper_item.id,
					survey_site: this.xmqdForm.survey_site,
					designer_id: this.xmqdForm.designer_id,
					designer_name: designerData ? designerData.name : '',
					survey_date: this.xmqdForm.survey_date
				})
				.then((res) => {
					console.log(res, '项目启动');
					uni.hideLoading();
					if (res.code == 200) {
						this.$emit('submit', this.value);
						this.close('projects_chooseSurveySite');
					}
					uni.showToast({
						title: res.message,
						icon: res.code == 200 ? 'success' : 'error'
					});
				});
		},
		// 发起结算
		confirmIsChangeItem() {
			let fqjsForm = this.fqjsForm;
			if (!fqjsForm.remark) {
				uni.showToast({
					title: '请输入备注',
					icon: 'error'
				});
				return;
			}
			uni.showLoading({
				mask: true
			});
			fqjsForm.project_id = this.oper_item.id;
			this.$Apipc.confirmIsChangeItem(fqjsForm).then((res) => {
				console.log(res, '发起结算');
				uni.hideLoading();
				if (res.code == 200) {
					this.$emit('submit', this.value);
					this.close('projects_confirmIsChangeItem');
				}
				uni.showToast({
					title: res.message,
					icon: res.code == 200 ? 'success' : 'error'
				});
			});
		},
		close(type) {
			switch (type) {
				case 'projects_assignAccountManager':
					this.fpkhShow = false;
					this.pfkhjlForm = {
						account_manager_id: '',
						account_manager_name: '',
						remark: ''
					};
					break;
				case 'common_updProjectLeader':
					this.changShow = false;
					break;
				case 'projects_projectRollback':
					this.xmhtShow = false;
					break;
				case 'projects_confirmIsChangeItem':
					this.fqjsShow = false;
					this.fqjsForm = {
						remark: ''
					};
					break;
				case 'projects_chooseSurveySite':
					this.xmqdShow = false;
					this.xmqdForm = {
						survey_site: 1,
						designer_id: '',
						survey_date: '',
						designer_name: ''
					};
					break;
				default:
			}
			this.$emit('update:value', '');
		},
		// 客户经理
		get_account_manager() {
			uni.showLoading({
				mask: true
			});
			var obj = {
				page: 1,
				num: 9999,
				keyword: '',
				province_id: '',
				city_id: '',
				county_id: '',
				identity: 1,
				position_id_arr: [2]
			};
			this.$Apipc
				.getAdminList(obj)
				.then((res) => {
					uni.hideLoading();
					console.log(res, '客户经理');
					if (res.code == 200) {
						this.accountManagerList = res.data.list;
						this.fpkhShow = true;
					} else {
						uni.showToast({
							title: res.message,
							icon: 'error'
						});
					}
				})
				.catch((err) => {});
		},
		// 分配客户经理
		assignAccountManager(e) {
			let pfkhjlForm = this.pfkhjlForm;
			if (!pfkhjlForm.account_manager_id) {
				uni.showToast({
					title: '请选择客户经理',
					icon: 'error'
				});
				return;
			}
			uni.showLoading({
				mask: true
			});
			this.$Api
				.assignAccountManager({
					project_id: this.oper_item.id,
					account_manager_id: pfkhjlForm.account_manager_id,
					remark: pfkhjlForm.remark
				})
				.then((res) => {
					console.log(res, '分配客户经理');
					uni.hideLoading();
					if (res.code == 200) {
						this.$emit('submit', this.value);
						this.close('projects_assignAccountManager');
					}
					uni.showToast({
						title: res.message,
						icon: res.code == 200 ? 'success' : 'error'
					});
				});
		},
		// 选择客户经理开启
		chooseKhjlFunc() {
			this.fpkhShow = false;
			this.fpkhShow2 = true;
		},
		// 选择客户经理关闭
		chooseKhjlFunc2() {
			this.fpkhShow = true;
			this.fpkhShow2 = false;
		},
		// 确认选择客户经理
		fpkhConfirm(e) {
			console.log(e);
			this.pfkhjlForm.account_manager_id = e.value[0].id;
			this.pfkhjlForm.account_manager_name = e.value[0].name;
			this.chooseKhjlFunc2();
		},
		// 更换客户经理完成
		changeManSuccessFunc() {
			this.$emit('submit', this.value);
			this.close('common_updProjectLeader');
		},
		//	更换客户经理弹窗关闭
		changCloseFunc() {
			this.close('common_updProjectLeader');
		},
		// 删除项目
		delProject(id_arr) {
			uni.showLoading({
				mask: true
			});
			this.$Apipc
				.delProject({ id_arr, remark: '' })
				.then((res) => {
					uni.hideLoading();
					console.log(res, '删除项目');
					if (res.code == 200) {
						this.$emit('submit', this.value);
					}
					this.close('projects_delProject');
					uni.showToast({
						title: res.code == 200 ? '删除成功' : res.message,
						icon: res.code == 200 ? 'success' : 'error'
					});
				})
				.catch((err) => {
					uni.showToast({
						title: err.message,
						icon: 'error'
					});
				});
		},
		// 项目回退弹窗关闭
		xmhtClose() {
			this.close('projects_projectRollback');
		},
		// 项目回退确认
		xmhtConfirm(e) {
			this.$Apipc
				.projectRollback({
					status: e.value[0].value,
					project_id: this.oper_item.id,
					remark: ''
				})
				.then((res) => {
					console.log(res, '回退');
					if (res.code == 200) {
						this.$emit('submit', this.value);
						this.close('projects_projectRollback');
					}
					uni.showToast({
						icon: res.code == 200 ? 'success' : 'error',
						title: res.code == 200 ? '处理成功' : res.message
					});
				})
				.catch((err) => {
					uni.showToast({
						icon: 'error',
						title: err.message
					});
				});
		}
	}
};
</script>

<style lang="scss" scoped>
.fp-box-title {
	height: 46px;
	border-bottom: 2rpx solid #efefef;
	text-align: center;
	line-height: 46px;
	font-size: 30rpx;
}
.fp-box {
	width: 100%;
	padding: 20rpx 30rpx 20rpx;
	box-sizing: border-box;
}
</style>
